
from backend.utils import Interp


Table15 = (
(71,     50,       0.7, 1.4, 1.3, 2.6, 1.0, 2.0, 1.3, 2.6, 2.0, 4.0, 0.4, 0.8),
(140,    100,      0.7, 1.4, 1.3, 2.6, 1.0, 2.0, 1.3, 2.6, 2.0, 4.0, 0.7, 1.4),
(210,    150,      0.9, 1.8, 1.3, 2.6, 1.0, 2.0, 1.3, 2.6, 2.0, 4.0, 0.7, 1.4),
(280,    200,      1.4, 2.8, 1.4, 2.8, 1.4, 2.8, 1.4, 2.8, 2.0, 4.0, 1.1, 2.2),
(420,    300,      1.9, 3.8, 1.9, 3.8, 1.9, 3.8, 1.9, 3.8, 2.0, 4.0, 1.4, 2.8),

(700,    500,      2.5, 5.0, 2.5, 5.0, 2.5, 5.0, 2.5, 5.0, 2.5, 5.0, 2.5, 5.0),
(840,    600,      3.2, 5.0, 3.2, 5.0, 3.2, 5.0, 3.2, 5.0, 3.2, 5.0, 3.2, 5.0),
(1400,   1000,     4.2, 5.0, 4.2, 5.0, 4.2, 5.0, 4.2, 5.0, 4.2, 5.0, 4.2, 5.0),
(2800,   2000,     8.4, 8.4, 8.4, 8.4, 8.4, 8.4, 8.4, 8.4, 8.4, 8.4, 8.4, 8.4),
(7000,   5000,     17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5),

(9800,   7000,     25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25),
(14000,  10000,    37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37),
(28000,  20000,    80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80),
(42000,  30000,    130,130,130,130,130,130,130,130,130,130,130,130,130,130,130),
)



def Table15_Process(pollution_class, 
              multiple_num, 
              voltage_type, 
              peak_voltage, 
              supply_voltage):
    
    if voltage_type == 0:
        Vidx = 0
    else:
        Vidx = 1

    if supply_voltage == 0:
        ECidx = 12
    elif supply_voltage <= 150:
        if pollution_class <= 1:
            ECidx = 2
        else:
            ECidx = 4
    elif supply_voltage <= 300:
        if pollution_class <= 1:
            ECidx = 6
        else:
            ECidx = 8
    else:
        ECidx = 10

    if multiple_num == 1:
        ECidx += 1

    if peak_voltage <= Table15[0][Vidx]:
        ElecClearance = Table15[0][ECidx]
    for i in range(1, len(Table15)):
        if peak_voltage > Table15[i-1][Vidx] and peak_voltage <= Table15[i][Vidx]:
            if peak_voltage > 2800:
                ElecClearance = Table15[i][ECidx]
            else:
                ElecClearance = Interp(Table15[i-1][Vidx],  Table15[i][Vidx],
                                       Table15[i-1][ECidx], Table15[i][ECidx],
                                       peak_voltage)

    return ElecClearance



